package pearson.automation.forms.registration;

import org.openqa.selenium.By;
import org.testng.Assert;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.CheckBox;
import framework.elements.Label;
import framework.elements.TextBox;

public class AccessInformationForm extends BaseForm {
    /*
     * Base form buttons
     */
    private Button next = new Button(
            By.id("next"), "Next");
    private static CheckBox noAccount = new CheckBox(
            By.id("accountNo"), "No account");
    private TextBox accessCode = new TextBox(
            By.id("access1"), "Access code");
    /*
     * A list of login attributes
     */
    private TextBox newLogin = new TextBox(
            By.id("new_smsuser_loginname"), "Login");
    private Button checkAvailability = new Button(
            By.id("checkAvailButton"), "Check Availability");
    private Label loginAvailable = new Label(
            By.id("nameIsAvailable"), "Login available");

    /*
     * A list of password attributes
     */
    private TextBox newPassword = new TextBox(
            By.name("new_smsuser_password"), "Password");
    private TextBox confirmPassword = new TextBox(
            By.name("smsuser_conf_password"), "Re-type Password");
    private Label passwordStrength = new Label(
            By.id("strength"), "Password strength");

    /*
     * Public constructor of the form
     */
    public AccessInformationForm() {
        super(noAccount.getLocator(), "Next");
    }

    /**
     * Step1 of the user's registration.
     *
     * @param login
     *            - User's Login(unic)
     * @param password
     *            - User's Password
     * @param passwordstrength
     *            - user password's strength
     * @param accessCodes
     *            - Unic code for eache platform
     * @return account informatio nform
     */
    public final AccountInformationForm fillRegistrationData(
            final String login,
            final String password,
            final String passwordstrength,
            final String accessCodes) {
        info("Fill registration data on step No2");
        noAccount.click();
        newLogin.type(login);

        // Checking that login is available
        checkAvailability.click();
        loginAvailable.assertIsPresent();

        // Typing password
        newPassword.type(password);
        confirmPassword.type(password);

        // Checking password strength
        passwordStrength.assertIsPresent();
        Assert.assertTrue(
                passwordStrength.getElement()
                    .getAttribute("class").toLowerCase()
                    .contains(passwordstrength),
            "Password strength is wrong");

        accessCode.type(accessCodes);
        next.clickAndWait();
        return new AccountInformationForm();
    }
}
